var windowName = "shopCat";
var Page,list;
apiready = function(){
    onWindowShow(function () {
        console.log(windowName + " show")
    })
    onWindowHide(function () {
        console.log(windowName + " hide")
    })
    console.log("index page is OK");
    list = new List();
    Page = createVM("#Page",{
        cats: []
        , curTab: 0
        , cat1: "0"
        , catName1: "家庭保洁"
        , cat2: "0"
        , catName2: "厨房清扫"
        ,limit:{"cat1Max":1,"cat2Max":3,"cat1Cur":0,"cat2Cur":0,"cat1":[],"cat2":[]}
        ,choice1:[]//一级分类的可选项
        ,choice2:[]//二级分类的可选项
        ,choose1:[]//一级分类的已选项
        ,choose2:[]//二级分类的已选项
        ,showCat1:true
        ,showCat2:true
        ,list:[]
        ,cat1list:[]
        ,cat2list:[]
    },{
        //页面方法
        "init":function () {
            this.cats = getLocData("cacheCats")
            var data = getLocData("shopCat");
            Page.limit = data
            Page.choose1 = isTrue(data.cat1)?data.cat1:[]
            Page.choose2 = isTrue(data.cat2)?data.cat2:[]
            Page.cat1list = isTrue(data.cat1En)?data.cat1En:[]
            Page.cat2list = isTrue(data.cat2En)?data.cat2En:[]
            setChoice2()

            getData()
            list.setUrl("/api/my/shopCategoryApplys")
                .setCbFunc(function (data) {
                    logJson(data)
                    Page.list = data
                })
                .getList();
        }

        ,tab:function (index) {
            log(index)
        }

        ,setCat1:function (index) {
            if (this.limit.cat1Max <= Page.choose1.length){showToast("已达到最大限额");return false;}
            if (!isInArray(Page.cats[index].id,Page.choose1)){
                Page.choose1.push(Page.cats[index].id)
            }
            setChoice2()
        }
        ,removeCat1:function (index) {
            var id = Page.cats[index].id;
            Page.choose1 = arrayDelValue(Page.choose1,id);
            setChoice2()
        }

        ,setCat2:function (index) {
            if (this.limit.cat2Max <= Page.choose2.length){
                showToast("已达到最大限额");
                return false;
            }
            if (!isInArray(Page.choice2[index].id,Page.choose2)){
                Page.choose2.push(Page.choice2[index].id)
            }
            this.limit.cat2Cur = Page.choose2.length
        }
        ,removeCat2:function (index) {
            var id = Page.choice2[index].id;
            Page.choose2 = arrayDelValue(Page.choose2,id);
            this.limit.cat2Cur = Page.choose2.length;
        }
        ,select:function (index) {
            switch (index) {
                case 1://设置主营行业
                    this.showCat1 = true
                    break;
                case 2://设置主营分类
                    this.showCat2 = true
                    break;
            }
        }

        ,submit:function () {
            var choose1 = this.choose1
            var choose2 = this.choose2
            apiPost("/app/my/shop/cat/apply",{cat1:choose1,cat2:choose2},function (res,code) {
                logJson(res,"/my/shop/cat/apply")
                if (code == 200) {
                    getData();
                    list.reload();
                }
                showToast(res.message)
            })
        }
    })
    Page.init()
}

function getData() {
    apiGet("/app/my/shop/cat",{},function (res,code) {

        setLocData("shopCat",res.data)

        logJson(res)
        Page.limit = res.data
        Page.choose1 = res.data.cat1
        Page.choose2 = res.data.cat2
        Page.cat1list = res.data.cat1En
        Page.cat2list = res.data.cat2En
        setChoice2()
    })
}

function setChoice2() {
    Page.choice2 = [];
    var choose2New = [];
    for (var i in Page.choose1) {
        var parentId =  Page.choose1[i];
        for (var index in Page.cats) {
            if (Page.cats[index].id == parentId) {
                for (var sonIndex in Page.cats[index].sons){
                    Page.choice2.push(Page.cats[index].sons[sonIndex])
                    var id = Page.cats[index].sons[sonIndex].id
                    if (isInArray(id,Page.choose2)) {
                        choose2New.push(id)
                    }
                }
            }
        }
    }
    Page.choose2= choose2New;
    Page.limit.cat1Cur = Page.choose1.length
    Page.limit.cat2Cur = Page.choose2.length;
}